package com.zmn.plat.business.impl.company;

import com.google.common.collect.Lists;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.business.interfaces.company.BaseCompanyBService;
import com.zmn.plat.business.interfaces.company.ManageCompanyAreaBService;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.servcategory.ServCategoryDRO;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.servcategory.ServCategory;
import com.zmn.plat.services.interfaces.area.BaseAreaService;
import com.zmn.plat.services.interfaces.company.BaseCompanyAreaService;
import com.zmn.plat.services.interfaces.servcategory.ServCategoryService;
import com.zmn.sp.dubbo.interfaces.rule.ServiceRuleCategoryListRemoteService;
import com.zmn.sp.dubbo.utils.DubboConsts;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 描述：服务公司校验是否可以服务BServiceImpl
 *
 * @author: tanghongjun
 * @time: 2020/1/8 14:54
 */
@Service
@Slf4j
public class ManageCompanyAreaBServiceImpl implements ManageCompanyAreaBService {

    @Autowired
    BaseAreaService baseAreaService;

    @Autowired
    BaseCompanyAreaService baseCompanyAreaService;

    @Autowired
    BaseCompanyBService baseCompanyBService;


    @Autowired
    ServCategoryService servCategoryService;

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private ServiceRuleCategoryListRemoteService serviceRuleCategoryListRemoteService;

    @Override
    public List<ServCategoryDRO> listServCategByManageCompanyIdAndCityId(Integer manageCompanyId, Integer cityId) throws PlatException {
        if (NumberUtil.isNullOrZero(manageCompanyId)) {
            log.error("manage company id null, manageCompanyId:{}", manageCompanyId);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        ResponseDTO<List<VtDTO>> listResponseDTO = serviceRuleCategoryListRemoteService.listServCategByCity(manageCompanyId, cityId);
        if (!listResponseDTO.isSuccess()) {
            throw new PlatException("服务商分类规则接口调用失败");
        }
        List<Integer> servCategIdList = Optional.ofNullable(listResponseDTO.getData()).orElse(Collections.emptyList())
                .stream()
                .map(VtDTO::getValue)
                .distinct()
                .collect(Collectors.toList());

        if (CollectionUtil.isNullOrEmpty(servCategIdList)) {
            return Collections.emptyList();
        }

        List<ServCategory> servCategoryList = servCategoryService.findByKeys(servCategIdList);
        List<ServCategoryDRO> dros = servCategoryList.stream().map(servCategory -> {
            ServCategoryDRO servCategoryDRO = new ServCategoryDRO();
            BeanUtils.copyProperties(servCategory, servCategoryDRO);
            return servCategoryDRO;
        }).collect(Collectors.toList());

        return dros;
    }

    @Override
    public List<VtDTO> listCategOneByManageCompanyIdAndServCategIdAndShowTypeAndCityId(Integer manageCompanyId, Integer servCategId, Integer cityId) throws PlatException {
        if (NumberUtil.isNullOrZero(manageCompanyId)) {
            log.error("manage company id null, manageCompanyId:{}", manageCompanyId);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        if (NumberUtil.isNullOrZero(servCategId)) {
            log.error("servCategId null, servCategId:{}", servCategId);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        ResponseDTO<List<VtDTO>> listResponseDTO = serviceRuleCategoryListRemoteService.listCategOneByShowCateg(manageCompanyId, servCategId, cityId);
        if (!listResponseDTO.isSuccess()) {
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_THIRD_SERVICE_ERROR);
        }
        List<VtDTO> data = Optional.ofNullable(listResponseDTO.getData()).orElse(Collections.emptyList());
        if (CollectionUtils.isEmpty(data)) {
            log.error("当前服务商没有配置对应产品分类,errorCode:{}", PlatErrorCodeDict.ERROR_CODE_SP_PRODUCT_CATEGORY_NOT_EXIST);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_SP_PRODUCT_CATEGORY_NOT_EXIST);
        }
        return data;

    }

    @Override
    public List<VtDTO> listCategTwoByManageCompanyIdAndServAndCategOneIdAndShowTypeAndCityId(Integer manageCompanyId, Integer servCategId, Integer categOneId, Integer cityId)
        throws PlatException {
        if (NumberUtil.isNullOrZero(manageCompanyId)) {
            log.error("manage company id null, manageCompanyId:{}", manageCompanyId);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        if (NumberUtil.isNullOrZero(servCategId)) {
            log.error("servCategId null, servCategId:{}", servCategId);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        if (NumberUtil.isNullOrZero(categOneId)) {
            log.error("categOneId null, categOneId:{}", categOneId);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        ResponseDTO<List<VtDTO>> listResponseDTO = serviceRuleCategoryListRemoteService.listCategTwoByShowCateg(manageCompanyId, servCategId, categOneId, cityId);
        if (!listResponseDTO.isSuccess()) {
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_THIRD_SERVICE_ERROR);
        }
        return Optional.ofNullable(listResponseDTO.getData()).orElse(Collections.emptyList());
    }

}
